package com.ngc.mall.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ngc.mall.common.configure.response.ResponseResult;
import com.ngc.mall.domain.MemberBenefitDomain;
import com.ngc.mall.domain.MemberLevelDomain;
import com.ngc.mall.domain.WxUserDomain;
import com.ngc.mall.dto.MemberFilterDTO;
import com.ngc.mall.service.IMemberBenefitService;
import com.ngc.mall.service.IMemberLevelService;
import com.ngc.mall.service.IWxUserService;
import com.ngc.mall.vo.MemberLevelVO;
import com.ngc.mall.vo.PagingVO;
import com.ngc.mall.vo.WxUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/member")
@Api(tags = "会员")
public class MemberController {

    @Resource
    private IWxUserService wxUserService;

    @Autowired
    private IMemberLevelService iMemberLevelService;

    @Autowired
    private IMemberBenefitService iMemberBenefitService;

    @GetMapping("/list")
    @ApiOperation("查看所有会员")
    public ResponseResult<List<WxUserDomain>> selectAllMember(){
        return ResponseResult.success(wxUserService.selectAllWxUser());
    }

    @GetMapping("/id/{id}")
    @ApiOperation("根据id查看会员详情")
    public ResponseResult<WxUserVO> getMemberDetailById(@PathVariable Long id){
        return ResponseResult.success(wxUserService.getDetailById(id));
    }


    @GetMapping("/level")
    @ApiOperation("获取会员等级")
    public ResponseResult<List<MemberLevelVO>> selectAllMemberLevel(){
        return ResponseResult.success(iMemberLevelService.findMemberLevel());
    }


    @PutMapping("/level/id/{id}")
    @ApiOperation("根据id更新会员等级")
    public ResponseResult<Boolean> updateLevelDetailById(@PathVariable Long id,@RequestBody MemberLevelDomain domain){
        return ResponseResult.auto(iMemberLevelService.update(domain,Wrappers.<MemberLevelDomain>lambdaUpdate().eq(MemberLevelDomain::getId,id)));
    }

    @PutMapping("/level")
    @ApiOperation("设置会员等级")
    public ResponseResult<Boolean> selectAllMemberLevel(@RequestBody List<MemberLevelDomain> memberLevelList){
        return ResponseResult.auto(iMemberLevelService.updateMemberLevel(memberLevelList));
    }

    @PostMapping("/search")
    @ApiOperation("模糊搜索用户")
    public ResponseResult<PagingVO<WxUserDomain>> search(@RequestBody MemberFilterDTO dto, @RequestParam(name = "start", defaultValue = "0")
            Integer start,
                                                         @RequestParam(name = "count", defaultValue = "8")
                                                                     Integer count){
        return ResponseResult.success(wxUserService.search(dto,start,count));
    }

    @GetMapping("/benefit/list")
    @ApiOperation("获取所有会员权益,按序列排序")
    public ResponseResult<List<MemberBenefitDomain>> queryAllMemberBenefit(){
        return ResponseResult.success(iMemberBenefitService.queryAll());
    }

    @PostMapping("/benefit")
    @ApiOperation("新增会员权益")
    public ResponseResult<Long> createMemberBenefit(@RequestBody MemberBenefitDomain domain){
        return ResponseResult.success(iMemberBenefitService.createMemberBenefit(domain));
    }

    @PutMapping("/benefit/id/{id}")
    @ApiOperation("根据id修改会员权益")
    public ResponseResult<Boolean> updateMemberBenefitById(@PathVariable Long id,@RequestBody MemberBenefitDomain domain){
        return ResponseResult.auto(iMemberBenefitService.updateById(id,domain));
    }

    @DeleteMapping("/benefit/id/{id}")
    @ApiOperation("根据id删除会员权益")
    public ResponseResult<Boolean> deleteMemberBenefitById(@PathVariable Long id){
        return ResponseResult.auto(iMemberBenefitService.deleteById(id));
    }

    @GetMapping("/benefit/id/{id}")
    @ApiOperation("根据id查看会员权益")
    public ResponseResult<MemberBenefitDomain> getMemberBenefitDetailById(@PathVariable Long id){
        return ResponseResult.success(iMemberBenefitService.getById(id));
    }

    @PostMapping("/id/{id}/relate_benefit")
    @ApiOperation("为指定会员等级关联权益")
    public ResponseResult<Boolean> relateMemberBenefitById(@PathVariable Long id,@RequestBody List<Long> benefitIdList ){
        return ResponseResult.auto(iMemberBenefitService.relateBenefitByMemberLevelId(id,benefitIdList));
    }

}
